package com.xigua._树;

import com.xigua.TreeNode;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
 * @date 2022/3/3022:32
 */
public class _226_翻转二叉树 {

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(4);
        TreeNode left = new TreeNode(2,new TreeNode(1),new TreeNode(3));
        TreeNode right = new TreeNode(7,new TreeNode(6),new TreeNode(9));
        treeNode.left = left;
        treeNode.right = right;
        treeNode = invertTree(treeNode);
    }

    public static TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        if (root.left == null && root.right == null) return root;
        TreeNode left = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(left);
        return root;
    }

}
